﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net; 
using System.IO;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

namespace SHKF.B2C.UpdateStock
{
    public class HttpHelp
    {
        private static string HttpRequest(string url, string json, string method)
        {
            string result = "";
            　
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = method; 
            req.ContentType = "application/json";

            #region 添加Post 参数
            if (!string.IsNullOrEmpty(json))
            {
                byte[] data = Encoding.UTF8.GetBytes(json);
                req.ContentLength = data.Length; 
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();
                }
            }
       
            #endregion

            SqlContext.Pipe.Send(url);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容            
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        public static string HttpRequestPost(string url, string json)
        {
           return HttpRequest(url, json, "POST");
        }

        public static string Get(string url)
        {
            return HttpRequest(url, "", "GET");
        }


    }
}
